package com.zyxx.common.mybatis;

/**
 * @author tomliu
 * @description: TODO
 * @date 2021/4/3021:10
 */

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zyxx.common.shiro.SingletonLoginUtils;
import com.zyxx.sys.entity.SysUserInfo;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 处理新增和更新的基础数据填充，配合BaseEntity和MyBatisPlusConfig使用
 */
@Component
public class MetaHandler implements MetaObjectHandler {


    /**
     * 新增数据执行
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        SysUserInfo userEntity = SingletonLoginUtils.getSysUserInfo();
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("createBy", userEntity.getAccount(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", userEntity.getAccount(), metaObject);
    }

    /**
     * 更新数据执行
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        SysUserInfo userEntity = SingletonLoginUtils.getSysUserInfo();
        this.setFieldValByName("updateTime", new Date(), metaObject);
        this.setFieldValByName("updateBy", userEntity.getAccount(), metaObject);
    }

}